package com.hsogoo.sharding.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;

/**
 * springboot 整合druid
 */
@Configuration
public class DataSourceConfig {

    @Value("${datasource.driverClassName}")
    private String driverClassName;
    @Value("${user.datasource.url}")
    private String userDbUrl;
    @Value("${user.datasource.password}")
    private String userDbPassword;
    @Value("${user.datasource.username}")
    private String userDbUsername;
    @Value("${order1.datasource.url}")
    private String order1DbUrl;
    @Value("${order1.datasource.password}")
    private String order1Password;
    @Value("${order1.datasource.username}")
    private String order1DbUsername;
    @Value("${order2.datasource.url}")
    private String order2DbUrl;
    @Value("${order2.datasource.password}")
    private String order2DbPassword;
    @Value("${order2.datasource.username}")
    private String order2DbUsername;

    @Bean
    @Primary
    public DataSource userDataSource(){

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(userDbUrl);
        dataSource.setUsername(userDbUsername);
        dataSource.setPassword(userDbPassword);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    @Bean
    public DataSource order1DataSource() {

        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(order1DbUrl);
        dataSource.setUsername(order1DbUsername);
        dataSource.setPassword(order1Password);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

    @Bean
    public DataSource order2DataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl(order2DbUrl);
        dataSource.setUsername(order2DbUsername);
        dataSource.setPassword(order2DbPassword);
        dataSource.setDriverClassName(driverClassName);
        return dataSource;
    }

}
